<%--
  Created by IntelliJ IDEA.
  User: xueal
  Date: 2018/1/16
  Time: 16:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.io.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>文件下载</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

</head>

<body>
<%
    try
    {
        //接受下载路径和文件名称
        String filepath = request.getParameter("filepath");
        String filename = request.getParameter("filename");
        if (filepath == null||filename ==null)
        {
            out.println("请选择要下载的文件！");
            return;
        }
        //获得读取本地文件的输入流
        FileInputStream fin = new FileInputStream(filepath);
        //设置响应的MIMI类型
        response.setContentType("application/force-download");
        response.addHeader("Content-Disposition", "attachment;filename="+filename);
        //流的方式输出文件
        byte[] buf = new byte[1024];
        int readSize = fin.read(buf);
        OutputStream os = response.getOutputStream();

        while(readSize != -1) {
            os.write(buf, 0, readSize);
            readSize = fin.read(buf);
        }
        os.flush();
        os.close();
        os = null ;
        response.flushBuffer();
        out.clear();
        out  =  pageContext.pushBody();
    }
    catch(Exception e){
        e.printStackTrace();
    }

%>
</body>
</html>
